package wait和notify方法.消费者与生产者模式.Test01;

import java.util.List;

//生产线程
public class Producer implements Runnable {
    private List list;

    public Producer(List list) {
        this.list = list;
    }

    @Override
    public void run() {
        int i = 0;
        while (true){
            synchronized (list){
                if (list.size() > 0){
                    try {
                        list.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                list.add(++i);
                System.out.println(Thread.currentThread().getName() + "生产了元素：" + i);
                try {
                    Thread.sleep(1000 * 2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                list.notify();
            }
        }
    }
}
